package com.xd100.lzll.gm.util;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtil {

	public static long MONTH_MILLIS = 30 * 24 * 60 * 60 * 1000L;
	public static long WEEK_MILLIS = 7 * 24 * 60 * 60 * 1000L;
	public static long DAY_MILLIS = 24 * 60 * 60 * 1000L;
	public static long HOUR_MILLIS = 60 * 60 * 1000L;
	public static long MIN_MILLIS = 60 * 1000L;
	public static long TIME_ZONE_OFFSET = 8;

	public static long getCurrentHour() {
		return (System.currentTimeMillis() % DateUtil.DAY_MILLIS) / 1000 / 60 / 60 + DateUtil.TIME_ZONE_OFFSET;
	}

	public static long getTomorrowMillis() {
		long millis = System.currentTimeMillis() - (System.currentTimeMillis() % DateUtil.DAY_MILLIS) + DAY_MILLIS - DateUtil.TIME_ZONE_OFFSET * HOUR_MILLIS;
		return millis;
	}

	public static Date getNowDate() {
		SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyy-MM-dd");
		String s = simpledateformat.format(new Date());
		return simpledateformat.parse(s, new ParsePosition(0));
	}
	
	public static Date stringToDate(String s) {
		try {
			return new SimpleDateFormat("yyyy-MM-dd").parse(s);
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
	}

	public static Date stringToFullDate(String s) {
		try {
			return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(s);
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 时间格式任意填写
	 * 
	 * @param s
	 *            鏃堕棿
	 * @return
	 */
	public static Date stringToFullDatepage(String timeformat, String s) {
		try {
			return new SimpleDateFormat(timeformat).parse(s);
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * yyyy-MM-dd HH:mm:ss
	 */
	public static String getStringDate() {
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date().getTime());
	}

	/**
	 * yyyy-MM-dd HH:mm:ss
	 */
	public static String getStringDate(long time) {
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time);
	}

	public static String getShortStringDate() {
		return new SimpleDateFormat("yyyy-MM-dd").format(new Date().getTime());
	}

	/**
	 * yyyy-MM-dd HH:mm:ss
	 */
	public static String getShortStringDate(long time) {
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time);
	}

	/**
	 * yyyy-MM-dd HH:mm:ss
	 */
	public static String dateToString(Date date) {
		String time = "";
		try {
			if (date != null) {
				time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
			}
		} catch (Exception e) {
			System.out.println("dateToString is exception");
		}
		return time;
	}

	/**
	 * yyy-MM-dd
	 */
	public static String dateToString2(Date date) {
		return new SimpleDateFormat("yyyy-MM-dd").format(date);
	}

	public static String sqlDateToString(java.sql.Date date) {
		java.sql.Timestamp ts = new java.sql.Timestamp(date.getTime());
		return ts.toString();
	}

	/**
	 * getNewDateForDay yyyy-MM-dd HH:mm:ss
	 */
	public static String getNewDateForDay(int day) {
		Calendar cal = new GregorianCalendar();
		cal.add(Calendar.DAY_OF_YEAR, day);
		return DateUtil.dateToString(cal.getTime());
	}

	/**
	 * getNewDateForHour yyyy-MM-dd HH:mm:ss
	 */
	public static String getNewDateForHour(int hour) {
		Calendar cal = new GregorianCalendar();
		cal.add(Calendar.HOUR_OF_DAY, hour);
		return DateUtil.dateToString(cal.getTime());
	}

	public static String getNewDateForHour(String oldDateStr, int hour) {
		Calendar cal = new GregorianCalendar();
		Date oldDate = stringToDate(oldDateStr);
		cal.setTime(oldDate);
		cal.add(Calendar.HOUR_OF_DAY, hour);
		return DateUtil.dateToString(cal.getTime());
	}

	/**
	 * getNewDateForMinute yyyy-MM-dd HH:mm:ss
	 */
	public static String getNewDateForMinute(int minute) {
		Calendar cal = new GregorianCalendar();
		cal.add(Calendar.MINUTE, minute);
		return DateUtil.dateToString(cal.getTime());
	}

	public static String getNewDateForMinute(String oldDateStr, int minute) {
		Calendar cal = new GregorianCalendar();
		Date oldDate = stringToDate(oldDateStr);
		cal.setTime(oldDate);
		cal.add(Calendar.MINUTE, minute);
		return DateUtil.dateToString(cal.getTime());
	}

	/**
	 * getNewDateForSecond yyyy-MM-dd HH:mm:ss
	 */
	public static String getNewDateForSecond(int second) {
		Calendar cal = new GregorianCalendar();
		cal.add(Calendar.SECOND, second);
		return DateUtil.dateToString(cal.getTime());
	}

	public static String getNewDateForSecond(String oldDateStr, int second) {
		Calendar cal = new GregorianCalendar();
		Date oldDate = stringToDate(oldDateStr);
		cal.setTime(oldDate);
		cal.add(Calendar.SECOND, second);
		return DateUtil.dateToString(cal.getTime());
	}

	public static Integer getMinutes(String s1, String s2) {
		Date d1 = DateUtil.stringToDate(s1);
		Date d2 = DateUtil.stringToDate(s2);
		return (int) ((d1.getTime() - d2.getTime()) / 1000 / 60);
	}

	public static Integer getMinutes(String s) {
		Date d1 = new Date();
		Date d2 = DateUtil.stringToDate(s);
		return (int) ((d1.getTime() - d2.getTime()) / 1000 / 60);
	}

	public static Integer getSeconds(String s) {
		Date d1 = new Date();
		Date d2 = DateUtil.stringToDate(s);
		return (int) ((d1.getTime() - d2.getTime()) / 1000);
	}

	public static boolean before(String stringDate) {
		Date now = new Date();
		return stringToDate(stringDate).before(now);
	}

	public static boolean after(String stringDate) {
		Date now = new Date();
		return stringToDate(stringDate).after(now);
	}

	/**
	 * getWeekDay
	 */
	public static int getWeekDay() {
		Calendar c = new GregorianCalendar();
		// c.add(c.DATE, 1);
		return c.get(Calendar.DAY_OF_WEEK) - 1;
	}

	public static boolean isTimeout(Date date) {
		Date now = new Date();
		try {
			if (date.getTime() / 60000 - now.getTime() / 60000 >= 0)
				return false;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public static boolean isNull(String name) {
		if (name != null && name != "")
			return false;
		return true;
	}

	public static Date ToDate(String date, String hour, String minute) {

		Date future = null;
		try {
			if (date == "" || date == null) {
				return new Date();
			}
			String d = date + " " + hour + ":" + minute + ":10";
			future = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(d);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return future;
	}

	public static boolean isEqualDay(String time) {
		String times[] = time.split(" ");
		if (times[0].equals(DateUtil.getShortStringDate()))
			return true;
		else
			return false;
	}

	public static Integer getSeconds(String s1, String s2) {
		Date d1 = DateUtil.stringToDate(s1);
		Date d2 = DateUtil.stringToDate(s2);
		return (int) ((d1.getTime() - d2.getTime()) / 1000);
	}

	public static String endTime(String end) {
		StringBuilder str = new StringBuilder(end);
		str.append(" 23:59:59");
		return str.toString();
	}

	public static String getTimestamp() {
		String date = getStringDate();
		date = date.replace("-", "");
		date = date.replace(":", "");
		date = date.replace(" ", "_");
		return date;
	}

	/**
	 * 得到某日期的前几日
	 * 
	 * @return
	 */
	public static String getDate(String str, int i) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Date dt = sdf.parse(str, new ParsePosition(0));
		Calendar rightNow = Calendar.getInstance();
		rightNow.setTime(dt);
		rightNow.add(Calendar.DATE, i);// 你要加减的日期
		// rightNow.roll(Calendar.DATE, 3);
		Date dt1 = rightNow.getTime();
		String reStr = sdf.format(dt1);
		reStr = reStr.substring(0, 4) + "-" + reStr.substring(4, 6) + "-" + reStr.substring(6, 8);
		return reStr;
	}
}
